# Create your views here.

from django.http import HttpResponse, HttpResponseRedirect
from models import Example
from models import Translation
from models import Entry

import bforms
from django.shortcuts import render_to_response

def render(template, payload):
    payload['recents'] = Entry.all().order('value').fetch(50)
    return render_to_response(template, payload)

def create_language(request):
  if request.method == 'GET':
    languageform = bforms.LanguageForm()
  if request.method == 'POST':
      languageform = bforms.LanguageForm(request.POST)
      if languageform.is_valid():
          verb = languageform.save()
      return HttpResponseRedirect('')
  payload = dict(languageform = languageform)
  return render('create_language.html', payload)

def create_pos(request):
  if request.method == 'GET':
    posform = bforms.PosForm()
  if request.method == 'POST':
      posform = bforms.PosForm(request.POST)
      if posform.is_valid():
          pos = posform.save()
      return HttpResponseRedirect('')
  payload = dict(posform = posform)
  return render('create_pos.html', payload)

def create_entry(request):
  if request.method == 'GET':
    entryform = bforms.EntryForm()
  if request.method == 'POST':
      entryform = bforms.EntryForm(request.POST)
      if entryform.is_valid():
          entry = entryform.save()
      return HttpResponseRedirect(entry.get_absolute_url())
  payload = dict(entryform = entryform)
  return render('create_entry.html', payload)

def entry_detail(request, entry_key):
    entry = Entry.get(entry_key)
    translations = Translation.all().filter('entry = ', entry)
    payload = dict(entry = entry, translations = translations)
    return render('entry_details.html', payload)


def create_translation(request, entry_key):
  entry = Entry.get(entry_key)
  if request.method == 'GET':
    form = bforms.TranslationForm()
  if request.method == 'POST':
      form = bforms.TranslationForm(request.POST)
      if form.is_valid():
          translation = form.save()
	  translation.entry = entry
	  translation.save()
          return HttpResponseRedirect(entry.get_absolute_url())
      else:
          return('bugger')
  payload = dict(form = form, entry = entry)
  return render('create_translation.html', payload)

def translation_detail(request, translation_key):
    translation = Translation.get(translation_key)
    examples = Example.all().filter('translation = ', translation)
    payload = dict(translation = translation, examples = examples)
    return render('translation_details.html', payload)

def index(request):
  result = "Words:"
  entries = Entry.all()

  for entry in entries.fetch(limit=40):
    result += entry.value

  return HttpResponse(result)

def create(request, translation_key):
  if request.method == 'GET':
    exampleform = bforms.ExampleForm()
  if request.method == 'POST':
      exampleform = bforms.ExampleForm(request.POST)
      translation = Translation.get(translation_key)
      if exampleform.is_valid():
          example = exampleform.save()
	  example.translation = translation
	  example.save()
      return HttpResponseRedirect(translation.get_absolute_url())
  payload = dict(exampleform=exampleform)
  return render('create.html', payload)

def example_detail(request, example_key):
    example = Example.get(example_key)

    payload = dict(example = example)
    return render('example_details.html', payload)
